package com.zhangxin.aiInterview.manager.work_flow.generate_exam;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * 生成笔试题
 */
public class ExamGenerate {
    // Coze API配置
    private static final String API_URL = "https://api.coze.cn/v1/workflow/run";
    private static final String API_TOKEN = "pat_VuLyIHYIwEWyyHFQml4LKcjDKJACPqPZS1C3lVxeyH9dcKJMhS2ElXKcuOCkEtTC";
    private static final String WORKFLOW_ID = "7512681611763122176";

    public static void main(String[] args) {
        System.out.println(generateExam("https://zx-1343343346.cos.ap-chongqing.myqcloud.com/ai_interview/exam_question/%E7%A7%91%E5%A4%A7%E8%AE%AF%E9%A3%9E-%E6%99%BA%E8%83%BD%E7%B3%BB%E7%BB%9F-java%E5%90%8E%E7%AB%AF%E5%BC%80%E5%8F%91%E5%AE%9E%E4%B9%A0.txt"));
    }

    public static String generateExam(String examUrl) {
        // 构建请求参数
        JSONObject params = new JSONObject();
        params.set("input", examUrl);

        JSONObject requestBody = new JSONObject();
        requestBody.set("parameters", params);
        requestBody.set("workflow_id", WORKFLOW_ID);
        requestBody.set("is_async", false);

        try {
            // 发送POST请求
            HttpResponse response = HttpRequest.post(API_URL)
                    .header("Authorization", "Bearer " + API_TOKEN)
                    .header("Content-Type", "application/json")
                    .body(requestBody.toString())
                    .execute();

            // 处理响应
            if (response.isOk()) {
                JSONObject responseBody = JSONUtil.parseObj(response.body());

                // 获取data字段
                String dataStr = responseBody.getStr("data");
                JSONObject dataObj = JSONUtil.parseObj(dataStr);

                // 获取output字段
                return dataObj.getStr("output");

            } else {
                System.err.println("请求失败，状态码：" + response.getStatus());
                System.err.println("错误信息：" + response.body());
            }
        } catch (Exception e) {
            System.err.println("请求发生异常：" + e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
}